﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Model;
using System.Collections;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace FantastPlatform.Community.UI.Service
{
    public partial class MailListType : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IMailListTypeService mltService = ServiceFactory.CreateService<IMailListTypeService>();
            ConditionGroup group = new ConditionGroup();
            group.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true });
            int PoliceManId = 0;
            if (LoginEmployee != null)
            {
                PoliceManId = LoginEmployee.Id;
                group.Conditions.Add(new Condition { Field = "PoliceMan.Id", Type = typeof(Int32).FullName, Value1 = LoginEmployee.Id, Relation = Relations.And });

                int totalCount = 0;
                switch (Action)
                {
                    case "Search":
                        IList<IMailListType> list = mltService.Search(group, null, PageSize, SelfPage, out totalCount);
                        object[] objArry = (from o in list
                                            select new
                                            {
                                                Id = o.Id,
                                                Name = o.Name,
                                                Code = o.Code,
                                                CreateDate = o.CreateDate.ToString(),
                                                Description = o.Description
                                            }
                                                ).ToArray();
                        ReturnList(totalCount, objArry);
                        break;
                    case "Add":
                        string data = Data;
                        MailListTypeView view = JsonToEntity<MailListTypeView>(RepairString(data));
                        IMailListType view2 = new FantastPlatform.Community.Model.MailListType();
                        if (view.Id == 0)
                        {
                            view2.Name = view.Name;
                            view2.Description = view.Description;
                            view2.CreateDate = DateTime.Now;
                            view2.Enable = true;
                            view2.PoliceMan = LoginEmployee;
                            mltService.Create(view2);
                            ReturnSuccessMessage("添加分组成功");
                        }
                        else
                        {
                            view2 = mltService.Get(view.Id);
                            view2.Name = view.Name;
                            view2.Description = view.Description;
                            view2.ModifyDate = DateTime.Now;
                            mltService.Update(view2);
                            ReturnSuccessMessage("修改分组成功");
                        }
                        break;
                    case "Get":
                        IMailListType mailListType = mltService.Get(Id);
                        ReturnObj(mailListType);
                        break;
                    case "Delete":
                        if (string.IsNullOrEmpty(Data))
                        {
                            throw new Exception("传入的设备信息ID为空");
                        }
                        List<int> idList = new List<int>();
                        string[] idStrList = Data.Split(',');
                        foreach (string id in idStrList)
                        {
                            idList.Add(int.Parse(id));
                        }
                        mltService.Delete(idList);
                        ReturnSuccessMessage("删除成功");
                        break;
                }
            }
        }

        public class MailListTypeView
        {
            public int Id { get; set; }
            public string Code { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    }
}